package com.cet.designpatterns.principle.demeter;

import java.util.ArrayList;
import java.util.List;

public class Demeter1 {

	public static void main(String[] args) {

		SchoolManager schoolManager = new SchoolManager();
		schoolManager.printAllEmployee(new CollegeManager());

	}

}

/**
 * 学校总部员工
 */
class Employee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
}

/**
 * 学院员工
 */
class CollegeEmployee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
}

/**
 * 学院员工管理类
 */
class CollegeManager {

	/**
	 * 返回学院所有员工
	 * @return
	 */
	public List<CollegeEmployee> getAllEmployee() {
		List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
		for (int i = 0; i < 10; i++) {
			CollegeEmployee emp = new CollegeEmployee();
			emp.setId("学院员工id= " + i);
			list.add(emp);
		}
		return list;
	}
}

/**
 * 学校管理类
 */
class SchoolManager {

	/*
	* 直接朋友：Employee、CollegeManager
	* 陌生类：CollegeEmployee
	* 陌生类违背了迪米特法则
	* */

	/**
	 * 返回学校总部员工
	 * @return
	 */
	public List<Employee> getAllEmployee() {
		List<Employee> list = new ArrayList<Employee>();

		for (int i = 0; i < 5; i++) {
			Employee emp = new Employee();
			emp.setId("学校总部员工id= " + i);
			list.add(emp);
		}
		return list;
	}

	/**
	 * 用于输出学校总部和学院员工信息的方法
	 * @param sub
	 */
	void printAllEmployee(CollegeManager sub) {

		// qz:
		// CollegeEmployee不是SchoolManager的直接朋友
		// CollegeEmployee是以局部变量的形式出现在SchoolManager中，违反了迪米特法则

		List<CollegeEmployee> list1 = sub.getAllEmployee();
		System.out.println("------------分公司员工------------");
		for (CollegeEmployee e : list1) {
			System.out.println(e.getId());
		}
		List<Employee> list2 = this.getAllEmployee();
		System.out.println("------------学校总部员工------------");
		for (Employee e : list2) {
			System.out.println(e.getId());
		}
	}
}
